package com.example.carapplication.entry;

import com.avos.avoscloud.AVException;
import com.avos.avoscloud.AVUser;
import com.avos.avoscloud.LogInCallback;
import com.example.carapplication.MainActivity;
import com.example.carapplication.R;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

public class LoginActivity extends Activity implements OnClickListener {
	private Button btn_loginbutton, btn_register;
	private EditText edt_lun, edt_lpw;
	private String username, password;
	private TextView tv_Pwd;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setContentView(R.layout.login);
		initView();
		btn_loginbutton.setOnClickListener(this);
		btn_register.setOnClickListener(this);
		tv_Pwd.setOnClickListener(this);

	}

	private void initView() {
		// TODO Auto-generated method stub
		btn_register = (Button) findViewById(R.id.btn_register);
		edt_lun = (EditText) findViewById(R.id.edt_username);
		edt_lpw = (EditText) findViewById(R.id.edt_password);

		btn_loginbutton = (Button) findViewById(R.id.btn_login);
		tv_Pwd = (TextView) findViewById(R.id.tv_forgetPWD);

	}

	@Override
	public void onClick(View v) {
		username = edt_lun.getText().toString();
		password = edt_lpw.getText().toString();
		switch (v.getId()) {
		case R.id.btn_login:
			AVUser.logInInBackground(username, password, new LogInCallback<AVUser>() {

				@Override
				public void done(AVUser user, AVException e) {
					// TODO Auto-generated method stub
					if (e == null) {
						Toast.makeText(LoginActivity.this, "登录成功", Toast.LENGTH_SHORT).show();
						Intent i = new Intent(LoginActivity.this, MainActivity.class);
						startActivity(i);
					} else {
						Toast.makeText(LoginActivity.this, "错误的用户名或密码，登录失败", Toast.LENGTH_SHORT).show();
					}

				}
			});
			break;
		case R.id.tv_forgetPWD:
			Intent j = new Intent(LoginActivity.this, ChangPasswordActivity.class);
			startActivity(j);
			break;
		case R.id.btn_register:
			Intent intent = new Intent(LoginActivity.this, RegisterActivity.class);
			startActivity(intent);
			break;
		}

	}

}
